{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#普通函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "price_array=[10,20,30,6.8,9.9]\n",
    "date_base=20180801\n",
    "date_array = [str(date_base + ind) for ind, _ in enumerate(price_array)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'20180801': 10, '20180802': 20, '20180803': 30, '20180804': 6.8, '20180805': 9.9}\n"
     ]
    }
   ],
   "source": [
    "stock_dict = {date:price for date, price in zip(date_array, price_array)}\n",
    "print(stock_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20180801'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(stock_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6.8, '20180804')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(zip(stock_dict.values(),stock_dict.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(20, '20180802')\n"
     ]
    }
   ],
   "source": [
    "#计算第二大的价格元素[-2];计算第二小的价格区间[1]\n",
    "def find_second_max(dict_array):\n",
    "    stock_prices_sorted = sorted(zip(dict_array.values(), dict_array.keys()))\n",
    "    return stock_prices_sorted[-2]\n",
    "\n",
    "if callable(find_second_max):\n",
    "    print(find_second_max(stock_dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((6.8, '20180804'), (30, '20180803'))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def find_max_and_min(dict_array):\n",
    "    stock_prices_sorted = sorted(zip(dict_array.values(), dict_array.keys()))\n",
    "    return stock_prices_sorted[0], stock_prices_sorted[-1]\n",
    "find_max_and_min(stock_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
